package com.ypd.shop.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;

import com.ypd.common.constant.AuctionConstants;
import com.ypd.shop.domain.AuctionEnrollment;
import com.ypd.shop.domain.AuctionItem;
import com.ypd.shop.service.IAuctionEnrollmentService;
import com.ypd.shop.service.IAuctionItemService;
import com.ypd.shop.service.IPostBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ypd.shop.mapper.AuctionStatusMapper;
import com.ypd.shop.domain.AuctionStatus;
import com.ypd.shop.service.IAuctionStatusService;

/**
 * 拍卖实时状态（如延时竞价）Service业务层处理
 * 
 * @author lin
 * @date 2025-02-28
 */
@Service
public class AuctionStatusServiceImpl implements IAuctionStatusService 
{
    @Autowired
    private AuctionStatusMapper auctionStatusMapper;
    @Autowired
    private IAuctionItemService auctionItemService;
    @Autowired
    private IPostBoxService postBoxService;
    @Autowired
    private IAuctionEnrollmentService auctionEnrollmentService;
    /**
     * 定时查看时间来修改状态的方法
     * @return
     */
    @Scheduled(cron="0/5 * *  * * ? ")
    public void updateStatus() {
        //对每个进行遍历,查看状态为1,2的,然后看是否需要进行更改状态
        List<AuctionItem> items = auctionItemService.selectAuctionItemList(new AuctionItem());
        for (AuctionItem item : items) {
            AuctionStatus status = auctionStatusMapper.selectAuctionStatusByItemId(item.getItemId());
            //每五秒进行判断,是否到时间
            //如果到了开始时间,1变2
            LocalDateTime now = LocalDateTime.now();
            if (now.compareTo(item.getStartTime())>=0&&item.getStatus()==AuctionConstants.AUCTION_STATUS_WAIT){
                item.setStatus(AuctionConstants.AUCTION_STATUS_BIDDING);
                //发送邮件
                sending(item,AuctionConstants.EVENT_START_NUM);
                //将数据放入数据库
                auctionItemService.updateAuctionItem(item);
                //如果到了结束时间,进行判断是否流拍,来决定是2变3还是2变4
            }
            else if (now.compareTo(status.getEndTime())>=0&&item.getStatus()==AuctionConstants.AUCTION_STATUS_BIDDING){
                if (status.getCurrentPrice().compareTo(item.getReservePrice())>=0){
                    item.setStatus(AuctionConstants.AUCTION_STATUS_END);
                    sending(item,AuctionConstants.EVENT_END_NUM);
                }else {
                    item.setStatus(AuctionConstants.AUCTION_STATUS_RERUN);
                    sending(item,AuctionConstants.EVENT_RERUN_NUM);
                }
                //将数据放入数据库
                auctionItemService.updateAuctionItem(item);
            }
        }
    }
    private  void sending(AuctionItem item, Integer event){
        //获取报名用户list
        List<String> userIds = auctionEnrollmentService.
                selectAuctionEnrollmentList(AuctionEnrollment.builder().itemId(item.getItemId()).build())
                .stream().map(AuctionEnrollment::getUserId).collect(Collectors.toList())    ;
        postBoxService.sendMail(item.getItemId(),userIds,AuctionConstants.EVENT_START_NUM);
    }


    /**
     * 查询拍卖实时状态（如延时竞价）
     * 
     * @param itemId 拍卖实时状态（如延时竞价）主键
     * @return 拍卖实时状态（如延时竞价）
     */
    @Override
    public AuctionStatus selectAuctionStatusByItemId(String itemId)
    {
        return auctionStatusMapper.selectAuctionStatusByItemId(itemId);
    }

    /**
     * 查询拍卖实时状态（如延时竞价）列表
     * 
     * @param auctionStatus 拍卖实时状态（如延时竞价）
     * @return 拍卖实时状态（如延时竞价）
     */
    @Override
    public List<AuctionStatus> selectAuctionStatusList(AuctionStatus auctionStatus)
    {
        return auctionStatusMapper.selectAuctionStatusList(auctionStatus);
    }

    /**
     * 新增拍卖实时状态（如延时竞价）
     * 
     * @param auctionStatus 拍卖实时状态（如延时竞价）
     * @return 结果
     */
    @Override
    public int insertAuctionStatus(AuctionStatus auctionStatus)
    {
        return auctionStatusMapper.insertAuctionStatus(auctionStatus);
    }

    /**
     * 修改拍卖实时状态（如延时竞价）
     * 
     * @param auctionStatus 拍卖实时状态（如延时竞价）
     * @return 结果
     */
    @Override
    public int updateAuctionStatus(AuctionStatus auctionStatus)
    {
        return auctionStatusMapper.updateAuctionStatus(auctionStatus);
    }

    /**
     * 批量删除拍卖实时状态（如延时竞价）
     * 
     * @param itemIds 需要删除的拍卖实时状态（如延时竞价）主键
     * @return 结果
     */
    @Override
    public int deleteAuctionStatusByItemIds(String[] itemIds)
    {
        return auctionStatusMapper.deleteAuctionStatusByItemIds(itemIds);
    }

    /**
     * 删除拍卖实时状态（如延时竞价）信息
     * 
     * @param itemId 拍卖实时状态（如延时竞价）主键
     * @return 结果
     */
    @Override
    public int deleteAuctionStatusByItemId(String itemId)
    {
        return auctionStatusMapper.deleteAuctionStatusByItemId(itemId);
    }
}
